// qpidclient.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <qpid/messaging/Address.h>
#include <qpid/messaging/Connection.h>
#include <qpid/messaging/Message.h>
#include <qpid/messaging/Receiver.h>
#include <qpid/messaging/Sender.h>
#include <qpid/messaging/Session.h>

#include <cstdlib>
#include <iostream>

#include <sstream>

using namespace qpid::messaging;

using std::stringstream;
using std::string;

int main(int argc, char** argv) {
	const char* url = argc > 1 ? argv[1] : "amqp:tcp:127.0.0.1:5672";
	std::string connectionOptions = argc > 2 ? argv[2] : "";

	Connection connection(url, connectionOptions);
	try {
		connection.open();
		Session session = connection.createSession();

		Sender sender = session.createSender("amq.topic");

		//create temp queue & receiver...
		Receiver receiver = session.createReceiver("#");
		Address responseQueue = receiver.getAddress();

		Message request;
		request.setReplyTo(responseQueue);
		
		do {
			std::cout << "please input request string(q:exit):";
			std::string input;
			std::getline(std::cin, input);
			if (input == "q") {
				break;
			}

			request.setContentBytes(input);
			sender.send(request);

			Message response = receiver.fetch();
			std::cout << "receive:" << response.getContentBytes() << std::endl;
			session.acknowledge();
		} while (1);

		connection.close();
	}
	catch (const std::exception& error) {
		std::cout << error.what() << std::endl;
		connection.close();
	}

	system("pause");
	return 1;
}